<?php

// 获取: MAC地址, 计算机名
// $env = new extEnv(); echo $env->getMac();
// echo extEnv::getCpnm

class extEnv{

    var $res = array(); // 返回带有MAC地址的字串数组 
    var $macAddr = ''; 

    function getMac($res=''){
        if(!function_exists('exec')){
            return '(unKnow)';
        }
        if(IS_WIN){ 
            $this->macWindows();
        }else{
            $this->macLinux();
        }
        $temp = array(); 
        foreach($this->res as $value){ 
            if(preg_match("/[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f]/i", $value, $temp)){ 
                $this->macAddr = $temp[0]; 
                break; 
            } 
        }
        unset($temp); 
        return $this->macAddr; 
    }
    function macWindows(){ 
        @exec("ipconfig /all", $this->res); 
        if($this->res) 
            return $this->res; 
        else{ 
            $ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe"; 
            if(is_file($ipconfig)) 
                @exec($ipconfig." /all", $this->res); 
            else 
                @exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this->res); 
            return $this->res; 
        } 
    } 
    function macLinux(){ 
        @exec("ifconfig -a", $this->res); 
        return $this->res; 
    } 

    static function getIp(){
        $a = array('HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR', 'HTTP_CLIENT_IP');
        foreach($a as $v){
            if(!empty($_SERVER[$v])){ 
                return $_SERVER[$v]; 
            }
        }
        return '';
    }
    static function getCpnm($ip='', $def=''){
        if(!function_exists('exec')){
            return '(unKnow)';
        }
        $ip || $ip = self::getIp();
        $cname = exec("nbtstat -A $ip | find /i \"20\""); # 'nbtstat -A'.' '.$ip.' | find /i "20"'
        $ctemp = preg_split("/\s+/", trim($cname));
        return empty($ctemp[0]) ? $def : $ctemp[0];
    }

    static function userOS($ua = '') {
        if(!$ua){ $ua = Basic::envUA(); } 
        $isMob = Basic::isMobile(); 
        if($isMob){
            if(strpos($ua, "iPhone")) {
                $res = "iPhone"; // OS 11_2_1
                preg_match("/OS (\d+\_\d+)/", $ua, $it);
                if(!empty($it[1])){ $res .= "($it[1])"; }
            }elseif(strpos($ua, "iPad")) {
                $res = "iPad";
            }elseif(strpos($ua, "Huawei") || strpos($ua, "Honor")) { 
                $res = "HUAWEI";
            }elseif(strpos($ua, "SAMSUNG") || strpos($ua, "Galaxy")) { 
                $res = "SAMSUNG";
            }elseif(strpos($ua, "Vivo") || strpos($ua, "vivo")) {
                $res = "Vivo";
            }elseif(strpos($ua, "OPPO")) {
                $res = "OPPO";
            }elseif(strpos($ua, "Windows")) {
                $res = "Win(Mobile)";
            }elseif(strpos($ua, "Android")) {
                $res = "Android";
                preg_match("/Android (\d+\.\d+)/", $ua, $it);
                if(!empty($it[1])){ $res .= "($it[1])"; }
            }else{
                $res = "Unknow(Mobile)";
            }
        }else{
            if(strpos($ua, "Windows")) {
                $res = "Windows";
                $res .= self::winVer($ua);
            }elseif(strpos($ua, "Mac")) {
                $res = "Mac";
            }elseif(strpos($ua, "Apple")) { 
                $res = "Applepc";
            }elseif(strpos($ua, "Linux")) {
                $res = "Linux";
            }elseif(strpos($ua, "Unix")) {
                $res = "Unix";
            }elseif(strpos($ua, "BSD")) {
                $res = "BSD";
            }elseif(strpos($ua, "SunOS")) {
                $res = "SunOS";
            }elseif(strpos($ua, "OS / 2")) {
                $res = "OS/2";
            }else{
                $res = "Unknow(PC)"; 
            }
        }
        return $res;
    }

    static function winVer($ua = '') {
        if(function_exists('exec')){
            $info = exec("ver"); // 0.20~0.67 s
            if($info){ $ua = $info; }
        } 
        preg_match("/ (\d+\.\d+)/", $ua, $it);
        return empty($it[1]) ? '' : "($it[1])";
    }

    static function userBrowser($ua = '') {
        if(!$ua){ $ua = Basic::envUA(); } 
        // 常用
        if(strpos($ua, "Chromium")) {
            $res = "Chromium";
        }elseif(strpos($ua, "MicroMessenger")) {
            $res = "Wechat(Browser)";
        }elseif(strpos($ua, "QQBrowser")) {
            $res = "QQBrowser"; 
        // PC-IE
        }elseif(strpos($ua, "Edge")) {
            $res = "MS-Edge";
        }elseif(strpos($ua, "rv:11.0")) {
            $res = "MSIE(11.0)";
        }elseif(strpos($ua, "MSIE")) {
            $res = "MSIE";
            preg_match("/MSIE (\d+\.\d+)/", $ua, $it);
            if(!empty($it[1])){ $res .= "($it[1])"; }
        }elseif(strpos($ua, "Firefox")) {
            $res = "Firefox";
        // PC-Else
        }elseif(strpos($ua, "Chrome")) {
            $res = "Chrome"; // Chrome/109
            preg_match("/Chrome\/(\d+)\./", $ua, $it);
            if(!empty($it[1])){ $res .= "($it[1])"; }
        }elseif(strpos($ua, "Safari")) { // iPhone/iPad/Android-内置浏览器
            $res = "Safari"; 
            preg_match("/Safari\/(\d+)\./", $ua, $it);
            if(!empty($it[1])){ $res .= "($it[1])"; }
        }else{
            $res = "Unknow(Browser)"; 
        }
        return $res;
    }

    // css去var(兼容IE11)
    static function cssVar($css) {
        preg_match_all("/\-\-(\w+)\:([^\;]+)\;/i", $css, $itms);
        foreach($itms[1] as $ino=>$ik){
            $css = str_replace("var(--$ik)", $itms[2][$ino], $css);
        }
        return $css;
    }

}

/*

*/
